package cn.ulyer.common.utils;


import cn.hutool.core.map.MapUtil;
import cn.ulyer.common.exception.TokenInvalidException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {

    public static String generatorToken(String priKey, Map<String,Object> claims){
                return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(new Date())
                // 加密算法和密钥
                .signWith(SignatureAlgorithm.HS512, priKey)
                .compact();
    }

    public static Claims parseClaims(String priKey,String token){
        Claims claims ;
        try{
            claims = Jwts.parser().setSigningKey(priKey).parseClaimsJws(token).getBody();
        }catch (Exception  e){
            throw new TokenInvalidException(e.getMessage());
        }
        return claims;
    }


    public static void main(String[] args) {
        Map<String ,Object> claims = MapUtil.newHashMap();
        claims.put("id","dasafa");
        String token = generatorToken("13123214",claims);
        Map<String,Object> s = parseClaims("13123214",token);
        System.out.println(s);
    }

}
